package com.such.kit.datastructure.tree;

import java.util.ArrayList;
import java.util.List;
import com.such.kit.Callable;
import com.such.kit.datastructure.tree.bean.Node;
import com.such.kit.validate.ValidateCommons;

/**
 * @作者 SUCH
 * @日期 2017-3-13 下午7:50:03
 * @描述 树结构通用工具类
 * @param <T>
 */
public class TreeCommons {

	/**
	 * <p>方法名：createNode</p>
	 * <p>描述：创建节点</p>
	 * @param nodeObject 构建节点数据载体
	 * @param caller 回调函数，返回字符串数组，数组长度为 3，依次存放 [id, parentId, title]
	 * @return 节点
	 * @throws Exception 
	 */
	public static <T> Node<T> createNode(T nodeObject, Callable<T, String[]> caller) 
			throws Exception {
		String[] array = caller.call(nodeObject);
		Node<T> node = new Node<T>(array[0], array[1], array[2]);
		node.setNodeObject(nodeObject);
		return node;
	}

	/**
	 * <p>方法名：createNodes</p>
	 * <p>描述：批量创建节点</p>
	 * @param nodeObjects 构建节点数据载体集合
	 * @param caller 回调函数，返回字符串数组，数组长度为 3，依次存放 [id, parentId, title]
	 * @return 节点集合
	 * @throws Exception 
	 */
	public static <T> List<Node<T>> createNodes(List<T> nodeObjects, Callable<T, String[]> caller) 
			throws Exception {
		List<Node<T>> nodes = new ArrayList<Node<T>>();
		if (ValidateCommons.isNotEmpty(nodeObjects)) {
			for (T nodeObject : nodeObjects) {
				nodes.add(createNode(nodeObject, caller));
			}
		}
		return nodes;
	}

}
